package timing.ukulele.third.service.weixin;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.chanjar.weixin.mp.api.WxMpQrcodeService;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.result.WxMpQrCodeTicket;
import org.springframework.stereotype.Service;
import timing.ukulele.data.third.data.weixin.WxAppDTO;
import timing.ukulele.data.third.view.weixin.WxAppVO;
import timing.ukulele.third.convertor.weixin.WxAppConverter;
import timing.ukulele.third.config.mp.WxMpConfiguration;
import timing.ukulele.third.mapper.wexin.WxAppMapper;
import timing.ukulele.third.persistent.weixin.WxApp;
import timing.ukulele.third.service.common.BaseOperatorService;
import java.util.List;

@Service
public class WxAppService extends BaseOperatorService<WxAppMapper, WxApp> {

    /**
     * 新增微信公众号
     * @param wxAppDTO
     * @return
     */
    public Boolean saveWxApp(WxAppDTO wxAppDTO) {
        WxApp wxApp = WxAppConverter.INSTANCE.d2p(wxAppDTO);
        return this.save(wxApp);
    }

    /**
     * 修改微信公众号
     * @param wxAppDTO
     * @return
     */
    public Boolean updateWxApp(WxAppDTO wxAppDTO) {
        WxApp wxApp = WxAppConverter.INSTANCE.d2p(wxAppDTO);
        return this.updateById(wxApp);
    }

    /**
     * 通过ID获取VO详情
     * @param id
     * @return
     */
    public WxAppVO getDetails(Long id) {
        WxApp wxApp = this.getById(id);
        WxAppVO wxAppVO = WxAppConverter.INSTANCE.p2v(wxApp);
        return wxAppVO;
    }

    public List<WxAppVO> allList(String appType) {
        List<WxApp> list = this.lambdaQuery()
                .eq(WxApp::getDeleted, 0)
                .eq(WxApp::getAppType, appType)
                .list();
        return WxAppConverter.INSTANCE.p2vList(list);
    }

    /**
     * 分页查询
     * @param wxAppDTO
     * @return
     */
    public IPage<WxAppVO> pageList(WxAppDTO wxAppDTO) {
        Page pageParam = new Page(wxAppDTO.getCurrent(), wxAppDTO.getPageSize());
        Page page = this.lambdaQuery()
                .eq(WxApp::getDeleted, 0)
                .like(StrUtil.isNotBlank(wxAppDTO.getName()), WxApp::getName, wxAppDTO.getName())
                .page(pageParam);
        List list = WxAppConverter.INSTANCE.p2vList(page.getRecords());
        page.setRecords(list);
        return page;
    }

    /**
     * 生成公众号二维码
     * @param wxAppDTO
     * @return
     */
    public String generateQrCode(WxAppDTO wxAppDTO) throws Exception {
        WxMpQrcodeService wxMpQrcodeService = WxMpConfiguration.getMpServiceById(wxAppDTO.getId()).getQrcodeService();
        WxMpQrCodeTicket wxMpQrCodeTicket = wxMpQrcodeService.qrCodeCreateLastTicket(wxAppDTO.getSceneStr());
        return wxMpQrCodeTicket.getUrl();
    }

    /**
     * 生成并保存公众号二维码
     * @param wxAppDTO
     * @return
     */
    public String generateAndSaveQrCode(WxAppDTO wxAppDTO) throws Exception {
        WxMpQrcodeService wxMpQrcodeService = WxMpConfiguration.getMpServiceById(wxAppDTO.getId()).getQrcodeService();
        WxMpQrCodeTicket wxMpQrCodeTicket = wxMpQrcodeService.qrCodeCreateLastTicket(wxAppDTO.getSceneStr());
        WxApp wxApp = new WxApp();
        wxApp.setId(wxAppDTO.getId());
        wxApp.setQrCode(wxMpQrCodeTicket.getUrl());
        this.updateById(wxApp);
        return wxMpQrCodeTicket.getUrl();
    }

    /**
     * 微信接口次数进行清零
     * @param id
     * @return
     * @throws Exception
     */
    public Boolean wxApiResetToZero(Long id) throws Exception {
        // 获取微信账号设置
        WxApp wxApp = this.getById(id);
        WxMpService wxMpService = WxMpConfiguration.getMpServiceByAppId(wxApp.getAppId());
        wxMpService.clearQuota(wxApp.getAppId());
        return Boolean.TRUE;
    }

    /**
     * 获取access-token
     * @param id
     * @return
     * @throws Exception
     */
    public String getAccessToken(Long id) throws Exception {
        WxMpService wxMpService = WxMpConfiguration.getMpServiceById(id);
        return wxMpService.getAccessToken();
    }

    /**
     * 获取jsapi-ticket
     * @param id
     * @return
     * @throws Exception
     */
    public String getJsapiTicket(Long id) throws Exception {
        WxMpService wxMpService = WxMpConfiguration.getMpServiceById(id);
        return wxMpService.getJsapiTicket();
    }

    /**
     * 通过APPID获取微信公众号
     * @param appId
     * @return
     */
    public WxApp findEntityByAppId(String appId) {
        return this.lambdaQuery().eq(WxApp::getAppId, appId).one();
    }

    /**
     * 通过微信原始标识获取微信公众号
     * @param wexinSign
     * @return
     */
    public WxApp findEntityByWexinSign(String wexinSign) {
        return this.lambdaQuery().eq(WxApp::getWeixinSign, wexinSign).one();
    }

}
